var app = getApp();
var $ = app.globalData;
Page({
	data: {
		myoften: [{id: 0,img: "image/tuijianguanxi.png",title: "推荐关系",width: "36",height: "36"},
			{id: 1,img: "image/miaosha.png",title: "店铺入驻",width: "40",height: "40"},
			{id: 2,img: "image/liushuijilu.png",title: "流水记录",width: "32",height: "36"},
			{id: 3,img: "image/dizhiguanli.png",title: "地址管理",width: "28",height: "37"},
			{id: 4,img: "image/zhuanzengcaifu.png",title: "财富转增",width: "34",height: "34"},
			{id: 5,img: "image/xiugaimima.png",title: "修改密码",width: "39",height: "37"},
			{id: 6,img: "image/daizhuce.png",title: "代注册",width: "35",height: "38"},
			{id: 7,img: "image/zhifumima.png",title: "设置支付密码",width: "36",height: "40"},
			{id: 8,img: "image/wodeshoucang.png",title: "我的收藏",width: "39",height: "37"},
			{id: 9,img: "image/gouwuche.png",title: "购物车",width: "38",height: "35"},
			{id: 10,img: "image/guanyuwomen.png",title: "关于我们",width: "36",height: "36"},
			{id: 11,img: "image/lianxikefu.png",title: "联系客服",width: "35",height: "40"}
		],
		qiandoa:"签到>"
	},
	myoften: function(e) {
		var that = this
		if (!wx.getStorageSync('userinfo')) {
			wx.showToast({
				icon: 'none',
				title: "请先登录哦~",
				duration: 3000,
			})
		} else {
			var thisid = e.currentTarget.dataset.index
			switch (thisid) {
				case 0:wx.navigateTo({url: "recommend/recommend"});break;
				case 1:that.shenq();break;
				case 2:wx.navigateTo({url: "stream/stream"});break;
				case 3:wx.navigateTo({url: "myadd/myadd"});break;
				case 4:wx.navigateTo({url: "giving/giving"});break;
				case 5:wx.navigateTo({url: "forgot/forgot"});break;
				case 6:wx.navigateTo({url: "daizhuce/daizhuce"});break;
				case 7:wx.navigateTo({url: "paypsw/paupsw"});break;
				case 8:wx.navigateTo({url: "soucang/soucang"});break;
				case 9:wx.switchTab({url: "../shoppingcart/shoppingcart"});break;
				case 10:wx.navigateTo({url: ""});break;
				case 11:wx.navigateTo({url: ""});break;
				case 12:
				wx.redirectTo({
					url:"../business/index/index"
				});break;
			}
		}
	},
	// 订单
	order: function(e) {
		var thisid = e.currentTarget.dataset.id
		wx.navigateTo({
			url: "order/order?id=" + thisid
		})
	},
	// 去登录
	gotologin: function() {
		wx.navigateTo({
			url: "../login/login"
		})
	},
	// 提现/
	tixian:function(){
		wx.showToast({
			icon: 'none',
			title: "功能暂未开放~",
			duration: 3000,
		})
	},
	goinvset: function() {
		wx.navigateTo({
			url: "invest/invest"
		})
	},
	// 申请开店
	shenq:function(){
		wx.navigateTo({
			url:"keepshop/keepshop"
		})
	},
	// 个人信息
	edit:function(){
		wx.navigateTo({
			url:"myinfo/myinfo"
		})
	},
	// 注销
	logout: function() {
		var that = this
		wx.request({
			url: $.AdminUrl + "/YApi/loginout",
			success: function(data) {
				console.log(data.data)
				$.userinfo = null
				wx.showToast({
					title: data.data.msg,
					duration: 3000,
				})
				wx.removeStorage({
					key: 'userinfo',
					success(res) {
						that.setData({
							userinfo: null
						})
						that.onLoad()
					}
				})
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this
		// 获取用户信息
		wx.getStorage({
			key: "userinfo",
			success: function(data) {
				$.userinfo = data.data
				// 更新菜单
				var muv = that.data.myoften
				wx.request({
					url:$.AdminUrl+"/YApi/shop_index",
					data:{customerid:data.data.id},
					success:function(data){
						if(data.data.code){
							muv[1].id = 12;
							muv[1].title = "我的店铺";
							$.myshopid = data.data.obj.id
						}else{
							muv[1].id = 1;
							muv[1].title = "申请开店";
						}
						that.setData({
							myoften:muv
						})
					}
				})
				that.setData({
					userinfo: data.data
				})
				// 更新用户信息
				wx.request({
					url: $.AdminUrl + "/YApi/getMySelfByLoginname",
					data: {
						loginname: data.data.loginname
					},
					success: function(res) {
						that.setData({
							userinfo: res.data.obj,
							token:res.data.token
						})
						wx.setStorage({
							key: "userinfo",
							data: res.data.obj
						})
						// 获取签到记录
						// 判断今日是否已经签到
						wx.request({
							url:$.AdminUrl+"/YApi/signin_history",
							data:{
								customerid:res.data.obj.id
							},
							success:function(res){
								if(res.data.msg == "今日已签到"){
									that.setData({
										qiandoa:'已签到'
									})
								}else{
									that.setData({
										qiandoa:'签到>'
									})
								}
							}
						})
					}
				})
				console.log(data)
			},
			fail: function() {
				wx.navigateTo({
					url: "../login/login"
				})
			}
		})
		
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		var that = this
		wx.getStorage({
			key: "userinfo",
			success: function(data) {
				// 更新菜单
				var muv = that.data.myoften
				wx.request({
					url:$.AdminUrl+"/YApi/shop_index",
					data:{customerid:data.data.id},
					success:function(data){
						if(data.data.code){
							muv[1].id = 12;
							muv[1].title = "我的店铺";
							$.myshopid = data.data.obj.id
						}else{
							muv[1].id = 1;
							muv[1].title = "申请开店";
						}
						that.setData({
							myoften:muv
						})
					}
				})
				// 更新用户信息
				wx.request({
					url: $.AdminUrl + "/YApi/getMySelfByLoginname",
					data: {
						loginname: data.data.loginname
					},
					success: function(res) {
						$.userinfo = res.data.obj
						that.setData({
							userinfo: res.data.obj,
							token:res.data.token
						})
						wx.setStorage({
							key: "userinfo",
							data: res.data.obj
						})
						
						// 获取签到记录
						// 判断今日是否已经签到
						wx.request({
							url:$.AdminUrl+"/YApi/signin_history",
							data:{
								customerid:res.data.obj.id
							},
							success:function(ress){
								if(ress.data.msg == "今日已签到"){
									that.setData({
										qiandoa:'已签到'
									})
								}
							}
						})
					}
				})
			}
		})
	},
	// 升级
	shengji:function(){
		wx.request({
			url:$.AdminUrl+"/YApi/setUpstr",
			data:{
				customerid:$.userinfo.id
			},
			success:function(res){
				wx.showToast({
					icon:"none",
					title:res.data.msg,
					duration: 3000,
				})
			}
		})
	},
	// 签到
	signin:function(){
		var that = this
		
		var userinfo = wx.getStorageSync('userinfo');
		if(!userinfo){
			wx.showToast({
				icon:"none",
				title: "您还未登录~",
				duration: 3000,
			})
		}else{
			// 获取签到记录
			wx.request({
				url:$.AdminUrl+"/YApi/signin",
				data:{
					customerid:$.userinfo.id
				},
				success:function(data){
					that.setData({
						qiandoa:'已签到'
					})
					wx.showToast({
						icon: 'none',
						title:data.data.msg,
						duration: 3000,
					})
					
				}
			})
		}
	},
	onShareAppMessage: function() {
		var userinfo = wx.getStorageSync('userinfo')
		return {
			title: '优悦购生活管家',
			path: '/pages/index/index?userid='+userinfo.loginname,
			success: (res) => {
				console.log("转发成功", res);
			},
			fail: (res) => {
				console.log("转发失败", res);
			}
		}
	}
})
